package com.batsoft.trade.api.web.auth;

import com.batsoft.trade.api.core.shiro.util.ShiroUtil;
import com.batsoft.trade.api.web.annotation.SysLog;
import com.batsoft.trade.api.web.auth.model.UserInfo;
import com.gomyb.result.R;
import com.gomyb.result.Result;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * @Copyright (c)</ b> HeC<br/>
 * @createTime 2018/11/3 22:28
 * @Description: 登录
 */
@RestController
@Log4j2
@RequestMapping(value = "/", name = "登录相关功能")
public class LoginController {


    /**
     * 登录校验
     */
    /*@SysLog("登录")
    @PostMapping(value = "/login", name = "登录")
    public Result login(AppUser user, @RequestParam(defaultValue = "false") Boolean rememberMe) {
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        //是否记住用户
        if (rememberMe) {
            token.setRememberMe(true);
        }
        //获得当前subject
        Subject currentUser = SecurityUtils.getSubject();
        //当前用户
        String username = user.getUsername();
        String msg = "登录成功";
        try {
            //登录校验
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            msg = "账户不存在";
        } catch (IncorrectCredentialsException ice) {
            msg = "密码不正确";
        } catch (DisabledAccountException lae) {
            msg = "账户未激活";
        } catch (ExcessiveAttemptsException eae) {
            msg = "用户名或密码错误次数过多";
        } catch (AuthenticationException ae) {
            // 通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景
            ae.printStackTrace();
            msg = "用户名或密码不正确";
        } catch (Exception e) {
            msg = e.getMessage();
            log.error(this.getClass(), e);
        }
        //验证登录状态
        if (currentUser.isAuthenticated()) {
            log.info("用户：{}  登录成功", username);
            currentUser.hasRole("");
            Map<String, Object> map = new HashMap<>(2);
            map.put("token", currentUser.getSession().getId());
            return R.OK(msg, map);
        } else {
            token.clear();
            return R.NO(msg);
        }
    }*/

    /**
     * 登陆状态验证
     */
    @SysLog("登录状态校验")
    @PostMapping(value = "/loginAuth", name = "登录状态校验")
    public Result loginAuth() {
        if (SecurityUtils.getSubject().isAuthenticated()) {
            return R.OK();
        } else {
            return R.NO();
        }
    }

    /**
     * 获得角色和权限
     */
    @PostMapping(value = "/getRoles", name = "获得角色和权限")
    public Result getRoles() {
        Set<String> roles = ShiroUtil.getRoles();
        Set<String> permissions = ShiroUtil.getPermissions();
        if (null != roles && null != permissions) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("roles", roles);
            map.put("permissions", permissions);
            return R.OK(map);
        }
        return R.NO();
    }

    /**
     * 用户信息
     */
    @PostMapping(value = "/user/info", name = "获得用户信息")
    public Result getUserInfo() {
        if (SecurityUtils.getSubject().isAuthenticated()) {
            Set<String> roles = ShiroUtil.getRoles();
            Set<String> permissions = ShiroUtil.getPermissions();
            UserInfo userInfo = new UserInfo();
            userInfo.setRoles(roles);
            userInfo.setPermissions(permissions);
            userInfo.setName(SecurityUtils.getSubject().getPrincipal() + "");
            userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            return R.OK(userInfo);
        }
        return R.NO();
    }

}
